package contact.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import contact.domain.Address;
import contact.domain.Contact;
import contact.domain.ContactGroup;
import contact.domain.DAOContact;
import contact.domain.DAOContactGroup;
import contact.domain.DAOEnterprise;
import contact.domain.DAOPhoneNumber;
import contact.domain.Enterprise;
import contact.domain.PhoneNumber;


public class NewContact extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    public NewContact() {
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		DAOContactGroup daoGroup = (DAOContactGroup) context.getBean("DAOContactGroup");
		DAOContact daoContact = (DAOContact) context.getBean("DAOContact");
		DAOPhoneNumber daoPhone = (DAOPhoneNumber) context.getBean("DAOPhoneNumber");
		DAOEnterprise daoEnterprise = (DAOEnterprise) context.getBean("DAOEnterprise");
		
		Address address = new Address(
				request.getParameter("street"),
				request.getParameter("city"),
				request.getParameter("zip"),
				request.getParameter("country"));
		
		boolean enterprise = Boolean.parseBoolean(request.getParameter("enterprise"));
		
		if (enterprise){
			Enterprise contact = new Enterprise(
					request.getParameter("firstname"),
					request.getParameter("lastname"),
					request.getParameter("email"),
					Integer.parseInt(request.getParameter("numsiret")));
			contact.setAddress(address);
			
			Long id = new Long(request.getParameter("idGroup"));
			ContactGroup group = daoGroup.read(id);
			group.getContacts().add(contact);
			contact.getBooks().add(group);
			
			String mobile = request.getParameter("mobile");
			String home = request.getParameter("home");
			String job = request.getParameter("job");
			
			daoEnterprise.create(contact);
			
			PhoneNumber phone = new PhoneNumber();
			phone.setContact(contact);
			phone.setPhoneKind(PhoneNumber.MOBILE);
			phone.setPhoneNumber(mobile);
			contact.getProfiles().add(phone);
			daoPhone.create(phone);
			phone = new PhoneNumber();
			phone.setContact(contact);
			phone.setPhoneKind(PhoneNumber.HOME);
			phone.setPhoneNumber(home);
			contact.getProfiles().add(phone);
			daoPhone.create(phone);
			phone = new PhoneNumber();
			phone.setContact(contact);
			phone.setPhoneKind(PhoneNumber.JOB);
			phone.setPhoneNumber(job);
			contact.getProfiles().add(phone);
			daoPhone.create(phone);
		}
		else{
			Contact contact = new Contact(
					request.getParameter("firstname"),
					request.getParameter("lastname"),
					request.getParameter("email"));
			contact.setAddress(address);
			
			Long id = new Long(request.getParameter("idGroup"));
			ContactGroup group = daoGroup.read(id);
			group.getContacts().add(contact);
			contact.getBooks().add(group);
			
			String mobile = request.getParameter("mobile");
			String home = request.getParameter("home");
			String job = request.getParameter("job");
			
			daoContact.create(contact);
			
			PhoneNumber phone = new PhoneNumber();
			phone.setContact(contact);
			phone.setPhoneKind(PhoneNumber.MOBILE);
			phone.setPhoneNumber(mobile);
			contact.getProfiles().add(phone);
			daoPhone.create(phone);
			phone = new PhoneNumber();
			phone.setContact(contact);
			phone.setPhoneKind(PhoneNumber.HOME);
			phone.setPhoneNumber(home);
			contact.getProfiles().add(phone);
			daoPhone.create(phone);
			phone = new PhoneNumber();
			phone.setContact(contact);
			phone.setPhoneKind(PhoneNumber.JOB);
			phone.setPhoneNumber(job);
			contact.getProfiles().add(phone);
			daoPhone.create(phone);
		}

		RequestDispatcher rd = request.getRequestDispatcher("/accueil.jsp");
		rd.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
